package com.smartfast4j.manager.modules.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smartfast4j.common.Constants;
import com.smartfast4j.common.annotation.LogRecord;
import com.smartfast4j.common.bean.R;
import com.smartfast4j.manager.common.bean.LoginUserInfo;
import com.smartfast4j.manager.common.controller.ManagerBaseController;
import com.smartfast4j.manager.modules.sys.service.SysLogService;
import com.smartfast4j.pojo.sys.SysLog;
import com.smartfast4j.pojo.sys.SysLogExample;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* SysLogController
*  由代码生成器生成
*/
@RestController
@RequestMapping("/sysLog")
public class SysLogController extends ManagerBaseController {

    @Autowired
    private SysLogService sysLogService;

    /**
     * 获取系统日志
     * @return
     */
    @RequestMapping(value = "/list")
    public R list(Integer offset, Integer limit, SysLog sysLog) {
        if (offset != null && limit != null) {
            //分页查询
            PageHelper.offsetPage(offset, limit);
        }
        SysLogExample example = new SysLogExample();
        SysLogExample.Criteria criteria = example.createCriteria();
        //只有超级管理员，才能查看所有，如果不是超级管理员，只能看到自己操作的日志记录
        if (!getLoginUserRoleIds().contains(Constants.SUPER_ADMIN)) {
            criteria.andUserIdEqualTo(getLoginUserId());
        }
        if (StringUtils.isNotEmpty(sysLog.getUsername())) {
            criteria.andUsernameLike("%" + sysLog.getUsername() + "%");
        }
        if (StringUtils.isNotEmpty(sysLog.getModuleName())) {
            criteria.andModuleNameEqualTo(sysLog.getModuleName());
        }
        if (sysLog.getLogType() != null && sysLog.getLogType()!=-1) {
            criteria.andLogTypeEqualTo(sysLog.getLogType());
        }
        example.setOrderByClause("create_time desc");
        List<SysLog> sysLogList = sysLogService.selectByExample(example);
        //分页信息
        PageInfo<SysLog> pageInfo = new PageInfo(sysLogList);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("total", pageInfo.getTotal());
        dataMap.put("rows", pageInfo.getList());
        //服务端分页，返回的结果必须包含total、rows两个参数。漏写或错写都会导致表格无法显示数据。
        return R.ok(dataMap);
    }


    /**
     * 分页查询登录日志
     * @param offset 从第几条开始取
     * @param limit 每次取多少条
     * @return
     */
    @RequestMapping(value = "/login/log")
    public R loginLog(Integer offset, Integer limit){
        if (offset != null && limit != null) {
            //分页查询
            PageHelper.offsetPage(offset, limit);
        }
        LoginUserInfo loginUserInfo = getCurrLoginUser();
        SysLogExample logExample = new SysLogExample();
        logExample.createCriteria().andLogTypeEqualTo(1).andUserIdEqualTo(loginUserInfo.getId());//登录日志
        logExample.setOrderByClause("create_time desc");
        List<SysLog> sysLogList = sysLogService.selectByExample(logExample);
        //分页信息
        PageInfo<SysLog> pageInfo = new PageInfo(sysLogList);
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("total",pageInfo.getTotal());
        dataMap.put("rows", pageInfo.getList());
        //服务端分页，返回的结果必须包含total、rows两个参数。漏写或错写都会导致表格无法显示数据。
        return R.ok(dataMap);
    }

    /**
     * 根据用户id批量删除用户
     * @param ids 要删除的用户ID，以逗号分隔
     * @return
     */
    @LogRecord(operation="删除登录日志",remark = "用户删除登录日志",moduleName = "系统日志")
    @RequestMapping(value = "/delete")
    public R delete(String ids){
        List<Long> idList = new ArrayList<>();
        if(StringUtils.isNotEmpty(ids)){
            String[] idArr = ids.split(",");
            for(String id:idArr){
                idList.add(Long.parseLong(id));
            }
        }
        //根据id删除
        SysLogExample logExample = new SysLogExample();
        logExample.createCriteria().andIdIn(idList);
        sysLogService.deleteByExample(logExample);//物理删除
        return R.ok();
    }

    /**
     * 获取模块名称
     * @return
     */
    @RequestMapping(value = "/getModuleNameList")
    public List<Map<String,String>> getModuleNameList(){
        List<String> moduleNameList = sysLogService.getModuleNameList();
        List<Map<String,String>> mapList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(moduleNameList)){
            for(String moduleName:moduleNameList){
                Map<String,String> map = new HashMap<>();
                map.put("id",moduleName);
                map.put("moduleName",moduleName);
                mapList.add(map);
            }
        }
        return mapList;
    }
}
